/* Controllers */
app.controller('homeCtrl',['$rootScope', '$scope','$location','$http', '$timeout', function ($rootScope, $scope, $location, $http, $timeout) {
    $timeout(function(){
        $scope.isLoading = 'fadeOut';
    }, 2000);
    $timeout(function(){
        $scope.isLoadend = true;
        $rootScope.isVisited = true;
    }, 3000);


    $scope.isView = false;
    $scope.isUpload = false;
    $scope.context = '';
    $scope.myImg = '';
    $scope.isUploadOk = false;
    $scope.uploadOkClass = '';


    $scope.cropArea = {
        isShow: false
    };
    $scope.blessData = {
        isShow: false,
        active: null,
        list: [
            '母亲节快乐，妈妈，我爱您！',
            '妈妈，虽然我老跟您发脾气，但我真的爱您!',
            '妈妈，感谢您多年的艰辛哺育，使我长大，使我成才!',
            '妈妈，您的怀抱最温暖。无论我走多远，心中永远眷恋您。',
            '走过了那么多年，我才深深知道，最爱我的人是您，妈妈。',
            '妈妈，不论您在哪儿，那里就是我们最快乐和向往的地方。',
            '是您指引我走出第一步，帮助我找到以后的路，谢谢您，妈妈',
            '妈，谢谢您给予我如此灿烂美好的生命！I love you forever!',
            '妈，节日快乐！知道了您为我操碎了心，母亲节我带对象回来见您！',
            '妈，节日快乐！我看中了个新玩意，回头给我打点费用，嘿嘿！'
        ]
    };
    $scope.toBless = function(){
        $scope.blessData.isShow = true;
    };
    $scope.blessClick = function(index){
        $scope.blessData.active = index;
    };
    $scope.selected = function(){
        $scope.blessData.isShow = false;
        $scope.context = $scope.blessData.list[$scope.blessData.active];
    };
    $scope.cancel = function(){
        $scope.blessData.isShow = false;
    };


    $scope.toSubmit = function(){
        var sendData = {
            imagePath: $scope.myImg,
            textContent:  $scope.context
        };
        var req = {
            method: 'POST',
            url: $config.host+'/index.php?s=/MotherDay/send',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            data: $config.toRequestUrl(sendData),
        }
        $http(req).then(
            function(json){
                //console.log(json);
                if(json.data.statusCode == 1){
                    $location.path('/show/'+json.data.data.id+'/'+json.data.data.sign);
                }else{
                    alert(json.data.message);
                }
            },
            function(err){
                console.log(err);
            }
        );
    };

    var pc = new PhotoClip('#clipArea', {
        size: [320, 240],
        //outputSize: 640,
        //adaptive: ['80%', '60%'],
        file: '#file',
        //view: '#view',
        ok: '#clipBtn',
        loadStart: function() {
            //console.log('开始读取照片');
        },
        loadComplete: function() {
            //console.log('照片读取完成');
            document.getElementById('clipbox').style.display = 'block';
            //$scope.isUpload = false;
        },
        done: function(dataURL){
            document.getElementById('clipbox').style.display = 'none';
            $scope.$apply(function(){
                $scope.isUpload = true;
            });
            //console.log(dataURL);
            var req = {
                method: 'POST',
                url: $config.host+'/index.php?s=/Upload/uploadBase64Common&channel=MotherDay',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                data: $config.toRequestUrl({imgsrc: dataURL}),
                //data: 'imgsrc='+dataURL
            }
            $http(req).then(
                function(json){
                    //console.log(json);
                    $timeout(function(){
                        if(json.data.errcode==0){
                            $scope.myImg = json.data.data[0];
                            $scope.isView = true;
                            $scope.isUpload = false;
                            $scope.isUploadOk = true;
                            $timeout(function(){
                                $scope.uploadOkClass = 'fadeOut';
                            }, 1500);
                            $timeout(function(){
                                $scope.isUploadOk = false;
                            }, 2500);
                        }
                    }, 2000);
                }
            );
        }
    });

    $scope.uploadImg = function(){
        $scope.cropArea.isShow = false;
    };
}]);


app.controller('mailCtrl',['$scope','$location', function ($scope,$location) {
    $scope.toShowPage = function(){
        $location.path('/show/'+$location.search().id+'/'+$location.search().sign);
    };
}]);

app.controller('showCtrl',['$rootScope', '$scope','$location', '$stateParams', '$http', '$timeout', function ($rootScope, $scope, $location, $stateParams, $http, $timeout) {
    $timeout(function(){
        $scope.isLoading = 'fadeOut';
    }, 2000);
    $timeout(function(){
        $scope.isLoadend = true;
        $rootScope.isVisited = true;
    }, 3000);
    var sendData = {
        id: $stateParams.id,
        sign: $stateParams.sign
    };
    var req = {
        method: 'GET',
        url: $config.host+'/index.php?s=/MotherDay/json4Content',
        params: sendData,
        //data: 'imgsrc='+dataURL
    }
    $http(req).then(
        function(json){
            if(json.data.statusCode==1){
                $scope.datas = json.data.data;
            }else{
                alert(json.data.message);
            }
        }
    );
    $scope.isPlay = true;
    $scope.musicClick = function(){
        if($scope.isPlay){
            $scope.isPlay = false;
            document.getElementById('music').pause();
        }else{
            $scope.isPlay = true;
            document.getElementById('music').play();
        }
    };


    var audio = document.getElementById('music');
    function audioAutoPlay(){
        document.addEventListener("WeixinJSBridgeReady", function () {
            audio.play();
        }, false);
        // document.addEventListener('YixinJSBridgeReady', function() {
        //     audio.play();
        // }, false);
        audio.play();
    }
    audioAutoPlay();
}]);